<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of wallet_model
 *
 * @author win7
 */
class stores_model extends admin_model {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->table = 'stores';
        $this->select = array('user.username', 'stores.* ');
        $this->order = array($this->table . '.id' => metohd_get_uri('sort', 'desc'));
        $this->join = array('user' => 'user.id=stores.owner_id');
        $this->columns_op = array(
            'user_id' => 'Callback_get_userid_with_username',
            'create_date >=' => 'Callback_get_date'
        );
    }

    function get_stat() {
        $pass = array();
        $pass['all_stores'] = $this->get(FALSE, TRUE, 'result', FALSE, FALSE, false, false, TRUE);
        $pass['trash_stores'] = $this->get(FALSE, TRUE, 'result', FALSE, FALSE, TRUE);
        $pass['active_stores'] = $this->get(array('active' => 'YES'), TRUE);
        $pass['notactive_stores'] = $this->get(array('active' => 'NO'), TRUE);
        $this->db->group_by('store_id');
        $pass['notcompleted_stores'] = $pass['all_stores'] - $this->db->get('store_details')->num_rows();
        $this->db->group_by('store_id');
        $temp = $this->db->get('store_subscribe')->result();
        $max = 0;
        $store_id = 0;
        foreach ($temp as $t) {
            $this->db->where('store_id', $t->store_id);
            $maxtemp = $this->db->get('store_subscribe')->num_rows();
            if ($maxtemp == $max) {
                if (!is_array($store_id))
                    $store_id = array();
                $store_id[] = $t->store_id;
            } elseif ($maxtemp > $max) {
                if (!is_numeric($store_id))
                    $store_id = false;
                $store_id = $t->store_id;
                $max = $maxtemp;
            }
        }
        $pass['topsub_stores'] = false;
        if (is_array($store_id)) {
            foreach ($store_id as $id)
                $pass['topsub_stores'] .= get_store_name_by_id($id) . ', ';
        } else {
            $pass['topsub_stores'] = get_store_name_by_id($store_id);
        }

        $this->db->group_by('store_id');
        $temp = $this->db->get('store_ratings')->result();
        $max = 0;
        $bottom = 0;
        $maxstore_id = 0;
        $bottomstore_id = 0;
        foreach ($temp as $t) {
            $resutemp = get_store_ratings($t->store_id);
            if (is_array($resutemp)) {
                if ($resutemp['type'] == 'positive') {
                    if ($resutemp['prec'] == $max && $resutemp['prec'] != 0) {
                        if (!is_array($store_id))
                            $maxstore_id = array();
                        $maxstore_id[] = $t->store_id;
                    } elseif ($resutemp['prec'] > $max) {
                        if (!is_numeric($maxstore_id))
                            $store_id = false;
                        $maxstore_id = $t->store_id;
                        $max = $resutemp['prec'];
                    }
                }elseif ($resutemp['type'] == 'negative') {
                    if ($resutemp['prec'] == $bottom && $resutemp['prec'] != 0) {
                        if (!is_array($bottomstore_id))
                            $bottomstore_id = array();
                        $bottomstore_id[] = $t->store_id;
                    } elseif ($resutemp['prec'] > $bottom) {
                        if (!is_numeric($bottomstore_id))
                            $bottomstore_id = false;
                        $bottomstore_id = $t->store_id;
                        $bottom = $resutemp['prec'];
                    }
                }
            }
        }
        $pass['toplike_stores'] = false;
        if (is_array($maxstore_id)) {
            foreach ($maxstore_id as $id)
                $pass['toplike_stores'] .= get_store_name_by_id($id) . ', ';
        } else {
            $pass['toplike_stores'] = get_store_name_by_id($maxstore_id);
        }
        $pass['likes'] = $max;

        $pass['bottomlike_stores'] = false;
        if (is_array($bottomstore_id)) {
            foreach ($bottomstore_id as $id)
                $pass['bottomlike_stores'] .= get_store_name_by_id($id) . ', ';
        } else {
            $pass['bottomlike_stores'] = get_store_name_by_id($bottomstore_id);
        }
        $pass['dislikes'] = $bottom;
        return $pass;
    }

    public function sfilter($nolimit = false) {
        /* ======== filter ======== */


        if (metohd_get_uri('username')) {
            $userid = get_userid_with_username(metohd_get_uri('username'));
            $this->db->where_in('wallet.user_id', $userid);
        }
        if (metohd_get_uri('id')) {

            $id = explode(',', metohd_get_uri('id'));

            $this->db->where_in('wallet.id', $id);
        }
        // status
        if (metohd_get_uri('status')) {
            $status = explode(',', metohd_get_uri('status'));
            $this->db->where_in('wallet.status', $status);
        }
        // operation
        if (metohd_get_uri('operation')) {
            $this->db->like('wallet.operation', metohd_get_uri('operation'));
        }
        // amount
        if (metohd_get_uri('amount')) {
            $this->db->like('wallet.amount', metohd_get_uri('amount'));
        }

        // Create_date
        if (metohd_get_uri('create_date')) {
            $date = date('Y-m-d H:i:s', strtotime(metohd_get_uri('create_date')));
            $this->db->where('wallet.create_date >=', $date);
        }




        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);


        $statrt_limit = get_segment(4, 0);
        if ($nolimit == false) {
            $this->db->limit($limit, $statrt_limit);
        }



        $sql = $this->db->get();

        return $sql->result();
    }

    public function confirm($data) {
        $update = array();
        $bank_discount = get_discount_bank();
        $update['status'] = 'active';
        $update['user_update'] = $this->admin_id;
        $out = metohd_get_uri('out');
        if (!empty($out)) {
            $update['discount'] = $bank_discount;
            $update['amount'] = ($data->amount - $bank_discount);
            $update['note'] = $data->note . ' ' . str_replace('%s', $bank_discount, lang('message_wallet_discount_15'));
        }
        $this->db->update('wallet', $update, array('id' => $data->id));
        //$this->email_system->SendSingelEmail('', $data->user_id, 'wallet_confirm', 'تم تاكيد  طلبكم الخاص بالمحفظة', $data = array('{ID}'=>$data->id));
    }

    public function rejected($id) {
        $update = array();
        $update['status'] = 'rejected';
        $update['admin_note'] = $this->input->post('note');
        $update['user_update'] = $this->admin_id;
        if (is_array($id)) {
            $this->db->where_in('id', $id);
        } else {
            $this->db->where('id', $id);
        }

        $this->db->update('wallet', $update, array());
    }

}
